<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Web Storage 示例</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }

        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }

        .card-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(450px, 1fr));
            gap: 25px;
            margin-top: 20px;
        }

        .card {
            background: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
        }

        .card h2 {
            color: #2575fc;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }

        .card p {
            margin-bottom: 15px;
            color: #555;
        }

        .code-block {
            background: #f8f9fa;
            border-left: 4px solid #2575fc;
            padding: 15px;
            margin: 15px 0;
            font-family: 'Courier New', monospace;
            white-space: pre-wrap;
            border-radius: 0 5px 5px 0;
            overflow-x: auto;
        }

        .btn {
            display: inline-block;
            background: #2575fc;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1rem;
            margin: 10px 5px 10px 0;
            transition: background 0.3s ease;
        }

        .btn:hover {
            background: #1a68e3;
        }

        .result {
            margin-top: 15px;
            padding: 15px;
            background: #f0f8ff;
            border-radius: 5px;
            border-left: 4px solid #4CAF50;
            display: none;
        }

        .result.show {
            display: block;
        }

        .storage-status {
            margin-top: 30px;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .storage-status h2 {
            color: #2575fc;
            margin-bottom: 15px;
        }

        .storage-items {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }

        .storage-item {
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #6a11cb;
        }

        footer {
            text-align: center;
            margin-top: 40px;
            padding: 20px;
            color: #666;
            font-size: 0.9rem;
        }

        @media (max-width: 768px) {
            .card-container {
                grid-template-columns: 1fr;
            }

            h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>Web Storage 示例</h1>
            <p class="subtitle">localStorage 存储和获取数据示例</p>
        </header>

        <div class="card-container">
            <!-- 示例1：存储基本数据类型 -->
            <div class="card">
                <h2>示例1：存储基本数据类型</h2>
                <p>使用localStorage存储字符串、数字和布尔值等基本数据类型。</p>

                <div class="code-block">// 存储基本数据类型
localStorage.setItem('name', '小明');
localStorage.setItem('age', 30);
localStorage.setItem('isStudent', true);</div>

                <button class="btn" id="storeBasicBtn">存储基本数据类型</button>
                <div class="result" id="storeBasicResult">
                    <strong>执行结果：</strong> 基本数据类型已成功存储到localStorage中。
                </div>
            </div>

            <!-- 示例2：获取基本数据类型 -->
            <div class="card">
                <h2>示例2：获取基本数据类型</h2>
                <p>从localStorage中获取存储的基本数据类型数据。</p>

                <div class="code-block">// 获取基本数据类型
const name = localStorage.getItem('name');
const age = localStorage.getItem('age');
const isStudent = localStorage.getItem('isStudent');</div>

                <button class="btn" id="getBasicBtn">获取基本数据类型</button>
                <div class="result" id="getBasicResult">
                    <strong>获取结果：</strong>
                    <div id="basicDataDisplay"></div>
                </div>
            </div>

            <!-- 示例3：存储复杂数据类型 -->
            <div class="card">
                <h2>示例3：存储复杂数据类型</h2>
                <p>使用JSON.stringify()将对象转换为字符串后存储到localStorage。</p>

                <div class="code-block">// 存储复杂数据类型（对象）
const user = {
    name: '张三',
    age: 25,
    hobbies: ['读书', '游泳', '编程'],
    isActive: true
};

localStorage.setItem('user', JSON.stringify(user));</div>

                <button class="btn" id="storeComplexBtn">存储复杂数据类型</button>
                <div class="result" id="storeComplexResult">
                    <strong>执行结果：</strong> 复杂数据类型已成功存储到localStorage中。
                </div>
            </div>

            <!-- 示例4：获取复杂数据类型 -->
            <div class="card">
                <h2>示例4：获取复杂数据类型</h2>
                <p>从localStorage中获取存储的复杂数据，并使用JSON.parse()转换为对象。</p>

                <div class="code-block">// 获取复杂数据类型
const userStr = localStorage.getItem('user');
const userObj = JSON.parse(userStr);</div>

                <button class="btn" id="getComplexBtn">获取复杂数据类型</button>
                <div class="result" id="getComplexResult">
                    <strong>获取结果：</strong>
                    <div id="complexDataDisplay"></div>
                </div>
            </div>
        </div>

        <div class="storage-status">
            <h2>当前localStorage内容</h2>
            <button class="btn" id="refreshStorageBtn">刷新查看</button>
            <button class="btn" id="clearStorageBtn">清空所有数据</button>
            <div class="storage-items" id="storageItems">
                <!-- 存储的内容将在这里显示 -->
            </div>
        </div>

        <footer>
            <p>Web Storage 示例 &copy; 2023 - 演示localStorage的基本用法</p>
        </footer>
    </div>

    <script>
        // 示例1：存储基本数据类型
        document.getElementById('storeBasicBtn').addEventListener('click', function() {
            try {
                localStorage.setItem('name', '小明');
                localStorage.setItem('age', 30);
                localStorage.setItem('isStudent', true);

                document.getElementById('storeBasicResult').classList.add('show');
                updateStorageDisplay();
            } catch (e) {
                alert('存储失败: ' + e.message);
            }
        });

        // 示例2：获取基本数据类型
        document.getElementById('getBasicBtn').addEventListener('click', function() {
            const name = localStorage.getItem('name');
            const age = localStorage.getItem('age');
            const isStudent = localStorage.getItem('isStudent');

            const display = document.getElementById('basicDataDisplay');
            display.innerHTML = `
                <p>姓名: ${name}</p>
                <p>年龄: ${age}</p>
                <p>是否是学生: ${isStudent}</p>
                <p><strong>注意：</strong> localStorage存储的所有数据都会转换为字符串形式。</p>
            `;

            document.getElementById('getBasicResult').classList.add('show');
        });

        // 示例3：存储复杂数据类型
        document.getElementById('storeComplexBtn').addEventListener('click', function() {
            try {
                const user = {
                    name: '张三',
                    age: 25,
                    hobbies: ['读书', '游泳', '编程'],
                    isActive: true
                };

                localStorage.setItem('user', JSON.stringify(user));
                document.getElementById('storeComplexResult').classList.add('show');
                updateStorageDisplay();
            } catch (e) {
                alert('存储失败: ' + e.message);
            }
        });

        // 示例4：获取复杂数据类型
        document.getElementById('getComplexBtn').addEventListener('click', function() {
            const userStr = localStorage.getItem('user');

            if (userStr) {
                const userObj = JSON.parse(userStr);

                const display = document.getElementById('complexDataDisplay');
                display.innerHTML = `
                    <p>姓名: ${userObj.name}</p>
                    <p>年龄: ${userObj.age}</p>
                    <p>爱好: ${userObj.hobbies.join(', ')}</p>
                    <p>是否活跃: ${userObj.isActive ? '是' : '否'}</p>
                    <p><strong>注意：</strong> 需要使用JSON.parse()将字符串转换回对象。</p>
                `;
            } else {
                const display = document.getElementById('complexDataDisplay');
                display.innerHTML = `<p>未找到用户数据，请先存储复杂数据类型。</p>`;
            }

            document.getElementById('getComplexResult').classList.add('show');
        });

        // 刷新存储内容显示
        document.getElementById('refreshStorageBtn').addEventListener('click', updateStorageDisplay);

        // 清空所有数据
        document.getElementById('clearStorageBtn').addEventListener('click', function() {
            if (confirm('确定要清空所有localStorage数据吗？')) {
                localStorage.clear();
                updateStorageDisplay();

                // 隐藏所有结果区域
                document.querySelectorAll('.result').forEach(result => {
                    result.classList.remove('show');
                });
            }
        });

        // 更新存储内容显示
        function updateStorageDisplay() {
            const storageItems = document.getElementById('storageItems');
            storageItems.innerHTML = '';

            for (let i = 0; i < localStorage.length; i++) {
                const key = localStorage.key(i);
                const value = localStorage.getItem(key);

                const item = document.createElement('div');
                item.className = 'storage-item';
                item.innerHTML = `
                    <strong>${key}:</strong><br>
                    <span>${value}</span>
                `;

                storageItems.appendChild(item);
            }

            if (localStorage.length === 0) {
                storageItems.innerHTML = '<p>localStorage中暂无数据</p>';
            }
        }

        // 初始化页面时显示当前存储内容
        updateStorageDisplay();
    </script>
</body>
</html>
